package com.situ.mall.base.util;

import com.situ.mall.base.vo.ResVO;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: AvatarUtil
 * Package: com.situ.mall.base.util
 * Description:
 *
 * @Author Darling_达
 * @Create 2023-03-18 14:22
 * @Version 1.0
 */
public class AvatarUtil {

    /**
     * 文件上传的封装
     *
     * @param file     欲上传的文件
     * @param path     上传文件的地址
     * @param filename 文件名
     * @return 返回vo，成功时vo的code为0，失败时vo的code为1
     */
    public synchronized ResVO avatar(MultipartFile file, String path, String filename) {
        ResVO vo = new ResVO();
        File f = new File(path + filename);
        try {
            file.transferTo(f);
            Map<String, String> map = new HashMap<>();
            map.put(C.SRC, filename);
            vo.setData(map);
        } catch (Exception e) {
            vo.setCode(1);
            throw new RuntimeException(e);
        }
        return vo;
    }

}
